package com.softeem.ossmusic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softeem.ossmusic.entity.Music;
import com.softeem.ossmusic.entity.Album;
import com.softeem.ossmusic.entity.User;
import com.softeem.ossmusic.entity.MusicList;
import com.softeem.ossmusic.service.MusicService;
import com.softeem.ossmusic.service.AlbumService;
import com.softeem.ossmusic.service.UserService;
import com.softeem.ossmusic.service.MusicListService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class DashboardController {

    @Autowired
    private MusicService musicService;
    
    @Autowired
    private AlbumService albumService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private MusicListService musicListService;

    /**
     * 获取统计数量
     */
    @GetMapping("/dashboard/stats")
    public Map<String, Object> getStats() {
        Map<String, Object> result = new HashMap<>();
        
        // 获取歌曲数量
        long musicCount = musicService.count();
        // 获取专辑数量
        long albumCount = albumService.count();
        // 获取歌单数量
        long musicListCount = musicListService.count();
        // 获取用户数量
        long userCount = userService.count();
        
        result.put("musicCount", musicCount);
        result.put("albumCount", albumCount);
        result.put("musicListCount", musicListCount);
        result.put("userCount", userCount);
        
        return result;
    }

    /**
     * 获取热搜歌曲TOP10（模拟数据）
     */
    @GetMapping("/dashboard/hotMusic")
    public List<Map<String, Object>> getHotMusic() {
        List<Map<String, Object>> hotMusicList = new ArrayList<>();
        
        // 模拟热搜歌曲数据
        String[] musicNames = {
            "孤勇者", "起风了", "踏山河", "万疆", "错位时空",
            "星辰大海", "白月光与朱砂痣", "可可托海的牧羊人", "海底", "你的答案"
        };
        
        for (int i = 0; i < musicNames.length; i++) {
            Map<String, Object> music = new HashMap<>();
            music.put("name", musicNames[i]);
            music.put("playCount", (int)(Math.random() * 10000) + 5000); // 随机播放量
            music.put("index", i + 1);
            hotMusicList.add(music);
        }
        
        // 按播放量降序排序
        hotMusicList.sort((a, b) -> Integer.compare((int)b.get("playCount"), (int)a.get("playCount")));
        
        return hotMusicList;
    }

    /**
     * 获取热搜专辑TOP10（模拟数据）
     */
    @GetMapping("/dashboard/hotAlbum")
    public List<Map<String, Object>> getHotAlbum() {
        List<Map<String, Object>> hotAlbumList = new ArrayList<>();
        
        // 模拟热搜专辑数据
        String[] albumNames = {
            "七里香", "范特西", "叶惠美", "Jay", "依然范特西",
            "最伟大的作品", "十一月的萧邦", "八度空间", "Jay Chou's Bedtime Stories", "魔杰座"
        };
        
        for (int i = 0; i < albumNames.length; i++) {
            Map<String, Object> album = new HashMap<>();
            album.put("name", albumNames[i]);
            album.put("singer", "周杰伦");
            album.put("listenCount", (int)(Math.random() * 5000) + 2000); // 随机收听量
            album.put("index", i + 1);
            hotAlbumList.add(album);
        }
        
        // 按收听量降序排序
        hotAlbumList.sort((a, b) -> Integer.compare((int)b.get("listenCount"), (int)a.get("listenCount")));
        
        return hotAlbumList;
    }

    /**
     * 获取热搜歌单TOP10（模拟数据）
     */
    @GetMapping("/dashboard/hotMusicList")
    public List<Map<String, Object>> getHotMusicList() {
        List<Map<String, Object>> hotMusicListList = new ArrayList<>();
        
        // 模拟热搜歌单数据
        String[] musicListNames = {
            "华语流行热歌榜", "欧美金曲精选", "轻音乐助眠", "抖音热歌TOP50", "经典老歌100首",
            "钢琴曲精选", "电音派对", "民谣小调", "影视原声", "嘻哈说唱"
        };
        
        for (int i = 0; i < musicListNames.length; i++) {
            Map<String, Object> musicList = new HashMap<>();
            musicList.put("name", musicListNames[i]);
            musicList.put("collectCount", (int)(Math.random() * 20000) + 10000); // 随机收藏量
            musicList.put("index", i + 1);
            hotMusicListList.add(musicList);
        }
        
        // 按收藏量降序排序
        hotMusicListList.sort((a, b) -> Integer.compare((int)b.get("collectCount"), (int)a.get("collectCount")));
        
        return hotMusicListList;
    }

    /**
     * 获取新增用户趋势数据（过去7天）- 使用实际用户数据
     */
    @GetMapping("/dashboard/userTrend")
    public Map<String, Object> getUserTrend() {
        Map<String, Object> result = new HashMap<>();
        List<String> dates = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        SimpleDateFormat fullSdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        
        // 统计过去7天的实际新增用户数
        for (int i = 6; i >= 0; i--) {
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            Date date = calendar.getTime();
            dates.add(sdf.format(date));
            
            // 获取当天的起始时间和结束时间
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(date);
            startCalendar.set(Calendar.HOUR_OF_DAY, 0);
            startCalendar.set(Calendar.MINUTE, 0);
            startCalendar.set(Calendar.SECOND, 0);
            startCalendar.set(Calendar.MILLISECOND, 0);
            Date startTime = startCalendar.getTime();
            
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(date);
            endCalendar.set(Calendar.HOUR_OF_DAY, 23);
            endCalendar.set(Calendar.MINUTE, 59);
            endCalendar.set(Calendar.SECOND, 59);
            endCalendar.set(Calendar.MILLISECOND, 999);
            Date endTime = endCalendar.getTime();
            
            // 查询当天创建的用户数量
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("create_time", startTime)
                       .le("create_time", endTime);
            long userCount = userService.count(queryWrapper);
            
            counts.add((int) userCount);
        }
        
        result.put("dates", dates);
        result.put("counts", counts);
        
        return result;
    }
}